function queryState(){
    fetch(server_url+http_host.queryServerState,{
        method:"POST",
        headers:post_header,
        body:JSON.stringify({
            verifyCode:window.sessionStorage.getItem("code")
        })
    }).then(da=>{return da.json();}).then(da=>{
        if(da.status){
            updateServerState(da);
        }
    });
}
function queryProperties(){
    fetch(server_url+http_host.querySaveProperties,{
        method:"POST",
        headers:post_header,
        body:JSON.stringify({
            verifyCode:window.sessionStorage.getItem("code")
        })
    }).then(da=>{return da.json();}).then(da=>{
        if(da.status)updateProperties(da.properties);
    });
}
function onSaveProperties(){

}
function updateProperties(daa){
    document.getElementById("name").value=daa.name;
    document.getElementById("content").value=daa.content;
    document.getElementById("maxOnlineNum").value=daa.maxOnlineNum;
    document.getElementById("autoSaveOpen").checked=daa.autoSaveOpen;
    document.getElementById("autoSaveInterval").value=daa.autoSaveInterval;
    document.getElementById("autoSaveUnit").value=daa.autoSaveUnit;
    document.getElementById("passwordTestOpen").checked=daa.passwordTestOpen;
    document.getElementById("passwordDefault").value=daa.passwordDefault;
    document.getElementById("showRoleOpen").checked=daa.showRoleOpen;
    document.getElementById("showRoleNum").value=daa.showRoleNum;
    document.getElementById("commandOpen").checked=daa.commandOpen;
    document.getElementById("nicknameRepetitionNPC").checked=daa.nicknameRepetitionNPC;
    document.getElementById("emailOpen").checked=daa.emailOpen;
    document.getElementById("emailAccount").value=daa.emailAccount;
    document.getElementById("emailPassword").value=daa.emailPassword;
    document.getElementById("emailCode").value=daa.emailCode;
    document.getElementById("emailSignature").value=daa.emailSignature;
    document.getElementById("prohibitedWordsOpen").checked=daa.prohibitedWordsOpen;
    document.getElementById("prohibitedWordsAPI").value=daa.prohibitedWordsAPI;
    document.getElementById("prohibitedWordsAppID").value=daa.prohibitedWordsAppID;
    document.getElementById("prohibitedWordsApiKey").value=daa.prohibitedWordsApiKey;
}
function updateServerState(da){
    let s=document.getElementById("state");
    s.innerHTML=da.run?"运行中":"已停止";
    s.style.color=da.run?"#50A000":"#E05000";
    document.getElementById("nowPlayerNum").innerHTML=da.nowPlayerNum;
    document.getElementById("maxPlayerNum").innerHTML=da.maxPlayerNum;
    document.getElementById("allPlayerNum").innerHTML=da.allPlayerNum;
    document.getElementById("serverID").innerHTML=da.serverID;
    document.getElementById("serverVersion").innerHTML=da.serverVersion;
    document.getElementById("button_run").innerHTML=da.run?"停止":"启动";
    document.getElementById("button_run").className=da.run?"button_red":"";
}
function onChangeServerState(event){
    event.target.disabled=true;
    event.target.className="button_disabled";
    if(event.target.innerHTML==="启动"){
        fetch(server_url+http_host.serverStart,{
            method:"POST",
            headers:post_header,
            body:JSON.stringify({
                verifyCode:window.sessionStorage.getItem("code")
            })
        }).then(da=>{return da.json()}).then(da=>{
            console.log(da);
            event.target.disabled=false;
            if(da.status){
                alert("启动成功");
                event.target.className="button_red";
                updateServerState(da);
            }else{
                alert("启动失败");
                event.target.className="";
            }
        })
    }else{
        fetch(server_url+http_host.serverStop,{
            method:"POST",
            headers:post_header,
            body:JSON.stringify({
                verifyCode:window.sessionStorage.getItem("code")
            })
        }).then(da=>{return da.json()}).then(da=>{
            event.target.disabled=false;
            if(da.status){
                alert("停止成功");
                event.target.className="button_red";
                updateServerState(da);
            }else{
                alert("停止失败");
                event.target.className="";
            }
        })
    }
}
function changeInputType(event){
    if(event.button!==1)return;
    if(event.target.type==="password")event.target.type="text";
    else event.target.type="password";
}
function copyServerID(event){
    navigator.clipboard.writeText("服务器ID："+event.target.innerHTML).then(()=>{
        alert("已复制服务器ID");
    })
}
queryState();
queryProperties();